package com.kedacom.freedomLabel.label;

import android.support.annotation.NonNull;

import com.kedacom.freedomLabel.AppExecutors;
import com.kedacom.freedomLabel.base.UserCache;
import com.kedacom.freedomLabel.label.annotation.FontScale;
import com.kedacom.freedomLabel.label.annotation.TemplateName;
import com.kedacom.freedomLabel.label.builder.GpLabelBuilder;
import com.kedacom.freedomLabel.label.column.TextLabelColumn;
import com.kedacom.freedomLabel.model.bean.Business;
import com.kedacom.freedomLabel.model.bean.Template;

/**
 * @author licheng
 * @since 2019/12/6 13:58
 */
public class TemplateManager {

    private static final String SEPARATOR = ":";
    private static TemplateManager sManager = null;

    private TemplateManager() {

    }

    public static TemplateManager getInstance() {
        if (sManager == null) {
            synchronized (TemplateManager.class) {
                if (sManager == null) {
                    sManager = new TemplateManager();
                }
            }
        }
        return sManager;
    }


    public static @TemplateName
    String getTemplateName(int index) {
        switch (index) {
            case 0:
                return TemplateName.LABEL_0;
            case 1:
                return TemplateName.LABEL_1;
            case 2:
                return TemplateName.LABEL_2;
            case 3:
                return TemplateName.LABEL_3;
            case 4:
                return TemplateName.LABEL_4;
            case 5:
                return TemplateName.LABEL_5;
        }
        return TemplateName.LABEL_1;
    }

    private String getValue(@NonNull String columnName, @NonNull Object value) {
        return String.format("%s%s%s", columnName, SEPARATOR, value);
    }

    // using thread
    public Label<Business> bindLabel(@NonNull Template template, @NonNull Business data) {
        // make sure using printer type? GpPrinter or ZebraPrinter
        GpLabel<Business> label = new GpLabelBuilder<Business>()
                .count(1)
                .data(data)
                .width(template.width)
                .height(template.height)
                .build();

        // basic information
        if (template.title != null) {
            TextLabelColumn column = new TextLabelColumn(template.title.x, template.title.y, data.category);
            label.addText(column, FontScale.SCALE_2, FontScale.SCALE_2);
        }
        if (template.qr != null) {

        }

        // id information
        if (template.id1 != null && data.itemValue1 != null) {
            String value = getValue(UserCache.customColumnNames[0], data.itemValue1);
            TextLabelColumn column = new TextLabelColumn(template.id1.x, template.id1.y, value);
            label.addText(column, FontScale.SCALE_1, FontScale.SCALE_1);
        }
        if (template.id2 != null && data.itemValue2 != null) {
            String value = getValue(UserCache.customColumnNames[1], data.itemValue2);
            TextLabelColumn column = new TextLabelColumn(template.id2.x, template.id2.y, value);
            label.addText(column, FontScale.SCALE_1, FontScale.SCALE_1);
        }
        if (template.id3 != null && data.itemValue3 != null) {
            String value = getValue(UserCache.customColumnNames[2], data.itemValue3);
            TextLabelColumn column = new TextLabelColumn(template.id3.x, template.id3.y, value);
            label.addText(column, FontScale.SCALE_1, FontScale.SCALE_1);
        }
        if (template.id4 != null && data.itemValue4 != null) {
            String value = getValue(UserCache.customColumnNames[3], data.itemValue4);
            TextLabelColumn column = new TextLabelColumn(template.id4.x, template.id4.y, value);
            label.addText(column, FontScale.SCALE_1, FontScale.SCALE_1);
        }
        if (template.id5 != null && data.itemValue5 != null) {
            String value = getValue(UserCache.customColumnNames[4], data.itemValue5);
            TextLabelColumn column = new TextLabelColumn(template.id5.x, template.id5.y, value);
            label.addText(column, FontScale.SCALE_1, FontScale.SCALE_1);
        }
        if (template.id6 != null && data.itemValue6 != null) {
            String value = getValue(UserCache.customColumnNames[5], data.itemValue6);
            TextLabelColumn column = new TextLabelColumn(template.id6.x, template.id6.y, value);
            label.addText(column, FontScale.SCALE_1, FontScale.SCALE_1);
        }

        // order information
        if (template.orderBatchNumber != null) {

        }
        if (template.orderSerialNumber != null) {

        }
        if (template.orderDealTime != null) {

        }

        // company information
        if (template.companyName != null && UserCache.companyName != null) {
            String value = getValue("companyName", UserCache.companyName);
            TextLabelColumn column = new TextLabelColumn(template.companyName.x, template.companyName.y, value);
            label.addText(column, FontScale.SCALE_1, FontScale.SCALE_1);
        }
        if (template.companyContactNumber != null && UserCache.companyTel != null) {
            String value = getValue("companyContactNumber", UserCache.companyTel);
            TextLabelColumn column = new TextLabelColumn(template.companyContactNumber.x, template.companyContactNumber.y, value);
            label.addText(column, FontScale.SCALE_1, FontScale.SCALE_1);
        }
        if (template.companyFaxNumber != null && UserCache.companyFax != null) {
            String value = getValue("companyFaxNumber", UserCache.companyFax);
            TextLabelColumn column = new TextLabelColumn(template.companyFaxNumber.x, template.companyFaxNumber.y, value);
            label.addText(column, FontScale.SCALE_1, FontScale.SCALE_1);
        }
        if (template.companyAddress != null && UserCache.companyAddress != null) {
            String value = getValue("companyAddress", UserCache.companyAddress);
            TextLabelColumn column = new TextLabelColumn(template.companyAddress.x, template.companyAddress.y, value);
            label.addText(column, FontScale.SCALE_1, FontScale.SCALE_1);
        }

        return label;
    }

    public void getTemplate(@TemplateName String templateName) {
        AppExecutors.getInstance().queryIO().execute(new Runnable() {
            @Override
            public void run() {

            }
        });
    }

    public void saveTemplate() {

    }

    public void deleteTemplate() {

    }

    public void deleteTemplate(@TemplateName String templateName) {

    }

    public static class Builder {

        Template mTemplate;

        public Builder(int width, int height) {
            this.mTemplate = new Template(width, height);
        }

        public Builder addTitle(int x, int y) {
            mTemplate.title = new Template.Position(x, y);
            return this;
        }

        public Builder addID1(int x, int y) {
            mTemplate.id1 = new Template.Position(x, y);
            return this;
        }

        public Builder addID2(int x, int y) {
            mTemplate.id2 = new Template.Position(x, y);
            return this;
        }

        public Builder addID3(int x, int y) {
            mTemplate.id3 = new Template.Position(x, y);
            return this;
        }

        public Builder addID4(int x, int y) {
            mTemplate.id4 = new Template.Position(x, y);
            return this;
        }

        public Builder addID5(int x, int y) {
            mTemplate.id5 = new Template.Position(x, y);
            return this;
        }

        public Builder addID6(int x, int y) {
            mTemplate.id6 = new Template.Position(x, y);
            return this;
        }

        public Builder addQR(int x, int y) {
            mTemplate.qr = new Template.Position(x, y);
            return this;
        }

        public Builder addOrderInformation(OrderPosition orderPosition) {
            mTemplate.orderBatchNumber = orderPosition.batch;
            mTemplate.orderSerialNumber = orderPosition.serial;
            mTemplate.orderDealTime = orderPosition.time;
            return this;
        }

        public Builder addCompanyInformation(CompanyPosition companyPosition) {
            mTemplate.companyName = companyPosition.name;
            mTemplate.companyFaxNumber = companyPosition.faxNumber;
            mTemplate.companyContactNumber = companyPosition.contactNumber;
            mTemplate.companyAddress = companyPosition.address;
            return this;
        }

        public Template build(@TemplateName String name) {
            mTemplate.type = name;
            return mTemplate;
        }
    }

    public static class CompanyPosition {
        public Template.Position name;
        public Template.Position contactNumber;
        public Template.Position faxNumber;
        public Template.Position address;
    }

    public static class OrderPosition {
        public Template.Position batch;
        public Template.Position serial;
        public Template.Position time;
    }
}